package main

import (
	"encoding/json"
	"fmt"
	"log"
	// "io/ioutil"
)

// Actress 女演员

//普通JSON
type Actress struct {
	Name       string
	Birthday   string
	BirthPlace string
	Opus       []string
}

// JSON内嵌普通JSON
/*
type Actress struct {
	Name       string
	Birthday   string
	BirthPlace string
	Opus       Opus
}
*/
// JSON内嵌数组JSON
/*
type Actress struct {
	Name       string
	Birthday   string
	BirthPlace string
	Opus       []Opus
}
*/

// JSON内嵌具有动态Key的JSON
//type Actress struct {
//	Name       string
//	Birthday   string
//	BirthPlace string
//	Opus       map[string]Opus
//}

// 作品Opus
type Opus struct {
	Date  string
	Title string
	Type  string
}

type Movie struct {
	Title string `json:"title"` //想换成json
	Year  int    `json:"year"`
	Price int    `json:"rmb"` //如果属性有json标签，那么转化成的json key就用该标签里的值，
	// 否则取属性名来作为key，也就是大写的Price,而不是`rmvb`
	Actors []string `json:"actors"` //演员切片
}

// 单次声明
type City string

func main() {
	/*我们先是介绍了Golang标准库的encoding/json包中的Unmarshal函数，然后通过上面4个示例代码，
	分别介绍了如何编码以下4种json格式数据
	*/
	
	/*
		//从文件中读取json数据：
		filePath := "src/resolveData.json"
		// 打开文件
		jsonFile, err := os.Open(filePath)
		if err != nil {
			fmt.Println("Error opening file: ", err)
		}
		defer jsonFile.Close()
	
		// 读取文件内容：从resolveData.json文件中读取json数据
		byteValue, _ := ioutil.ReadAll(jsonFile)
		obj := make(map[string]interface{})
		_ = json.Unmarshal([]byte(byteValue), &obj)
		//log.Println("解析准备提交时byteValue为：", byteValue)
		log.Println("解析准备提交时res.ResolveData为：", obj)
	*/
	
	otherjsonData := []byte(`{
    "custom": {
        "3rdPlugins": "[]"
    },
    "envVars": [],
    "ext": "QuickTime",
    "gpu": 0,
    "handled": 1,
    "keyFramesStrategy": 2,
    "mode": 0,
    "modelParameters": [
        {
            "cameras": [
                "测试卡顿"
            ],
            "ext": "QuickTime",
            "frames": "-2-4998",
            "height": 1280,
            "part": "测试卡顿",
            "selected": 0,
            "subFrame": 8,
            "width": 3258
        },
        {
            "cameras": [
                "合成文字02"
            ],
            "ext": "QuickTime",
            "frames": "0-5000",
            "height": 2160,
            "part": "合成文字02",
            "selected": 1,
            "subFrame": 8,
            "width": 3840
        },
        {
            "cameras": [
                "合成文字材质02"
            ],
            "ext": "QuickTime",
            "frames": "0-2500",
            "height": 2160,
            "part": "合成文字材质02",
            "selected": 1,
            "subFrame": 4,
            "width": 3840
        },
        {
            "cameras": [
                "贴图_金属鎏金"
            ],
            "ext": "QuickTime",
            "frames": "0-5000",
            "height": 2160,
            "part": "贴图_金属鎏金",
            "selected": 0,
            "subFrame": 8,
            "width": 3840
        },
        {
            "cameras": [
                "合成文字02修改 "
            ],
            "ext": "QuickTime",
            "frames": "0-5000",
            "height": 2160,
            "part": "合成文字02修改 ",
            "selected": 0,
            "subFrame": 8,
            "width": 3840
        }
    ],
    "name": "",
    "noResolve": 0,
    "plugins": [],
    "resourceId": 12219,
    "resourceName": "WLCB_PC2_16C32G_20230605",
    "resultRootPath": "",
    "scenePath": "C:\\Users\\0921ssk\\Desktop\\代码与数据\\模型文件\\Xrender_ssk乱码测试1.aepx",
    "scenes": [
        "C:\\Users\\0921ssk\\Desktop\\代码与数据\\模型文件\\Xrender_ssk乱码测试1.aepx"
    ],
    "software": "AfterEffects 2022",
    "softwareId": 6102,
    "softwareType": 17,
    "split": 1,
    "taskId": "890000008dd94a6e018e59e2515b099f",
    "taskName": "My task",
    "uploadStop": 0
}`)
	otherobj := make(map[string]interface{})
	_ = json.Unmarshal([]byte(otherjsonData), &otherobj)
	//log.Println("解析准备提交时otherjsonData为：", string(otherjsonData))
	log.Println("解析准备提交时res.ResolveData为：", otherobj)
	
	//newMap := make(map[string]interface{})
	//customMap := make(map[string]interface{})
	//customMap["AI_RENDER"] = "1"

	//_ := otherobj["custom"].(map[string]interface{})
	if cmap, ok := otherobj["custom"].(map[string]interface{}); ok {
		cmap["AI_RENDER"] = "1"
	} else {
		log.Println("envVars is not map")
		mmap := make(map[string]interface{})
		mmap["AI_RENDER"] = "1"
		otherobj["custom"] = mmap
		//
		//otherobj["custom"] = otherobj["custom"]
	}





	//newMap["AI_RENDER"] = "1"
	log.Println("AI双渲obj：", otherobj)



	//if _, ok := (*m)[key]; !ok {
	//	(*m)[key] = value
	//}
	
	ds := struct {
		AnalysisSuccess int
		AnalysisWarn    int
		IgnoreAnalysis  int
		MayaCommit      int //maya分层
	}{}
	structJudgeNilTest(&ds)
	
	city := City("上海")
	fmt.Println(city)
	
	//普通JSON    // 因为json.UnMarshal() 函数接收的参数是字节切片，   // 所以需要把JSON字符串转换成字节切片。
	jsonData := []byte(`{
			"name": "迪丽热巴",
			"birthday": "1992-06-03",
			"birthPlace": "新疆乌鲁木齐市",
			"opus": [
				"《阿娜尔罕》",
				"《逆光之恋》",
				"《克拉恋人》"
			]
		}`)
	var actress Actress
	errActor := json.Unmarshal(jsonData, &actress)
	if errActor != nil {
		fmt.Println("error:", errActor)
		return
	}
	fmt.Printf("姓名：%s\n", actress.Name)
	fmt.Printf("生日：%s\n", actress.Birthday)
	fmt.Printf("出生地：%s\n", actress.BirthPlace)
	fmt.Println("作品：")
	for _, val := range actress.Opus {
		fmt.Println("\t", val)
	}
	
	/*
		//JSON内嵌普通JSON
		jsonData := []byte(`{
			"name": "迪丽热巴",
			"birthday": "1992-06-03",
			"birthPlace": "新疆乌鲁木齐市",
			"opus": {
				"Date": "2013",
				"Title": "《阿娜尔罕》"
			}
		}`)
		var actress Actress
		errActor := json.Unmarshal(jsonData, &actress)
		if errActor != nil {
			fmt.Println("error:", errActor)
			return
		}
		fmt.Printf("姓名：%s\n", actress.Name)
		fmt.Printf("生日：%s\n", actress.Birthday)
		fmt.Printf("出生地：%s\n", actress.BirthPlace)
		fmt.Println("作品：")
		fmt.Printf("\t%s:%s", actress.Opus.Date, actress.Opus.Title)
	*/
	
	//JSON内嵌数组JSON
	/*
		jsonData := []byte(`{
			"name": "迪丽热巴",
			"birthday": "1992-06-03",
			"birthPlace": "新疆乌鲁木齐市",
			"opus": [
				{
					"date": "2013",
					"title": "《阿娜尔罕》"
				},
				{
					"date": "2014",
					"title": "《逆光之恋》"
				},
				{
					"date": "2015",
					"title": "《克拉恋人》"
				}
			]
		}`)
		var actress Actress
		errActor := json.Unmarshal(jsonData, &actress)
		if errActor != nil {
			fmt.Println("error", errActor)
			return
		}
		fmt.Printf("姓名：%s\n", actress.Name)
		fmt.Printf("生日：%s\n", actress.Birthday)
		fmt.Printf("出生地：%s\n", actress.BirthPlace)
		fmt.Println("作品：")
		for _, val := range actress.Opus {
			fmt.Printf("\t%s - %s\n", val.Date, val.Title)
		}
	*/
	//JSON内嵌具有动态Key的JSON
	/*
		jsonData := []byte(`{
			"name": "迪丽热巴",
			"birthday": "1992-06-03",
			"birthPlace": "新疆乌鲁木齐市",
			"opus": {
				"2013": {
					"Type": "近代革命剧",
					"Title": "《阿娜尔罕》"
				},
				"2014": {
					"Type": "奇幻剧",
					"Title": "《逆光之恋》"
				},
				"2015": {
					"Type": "爱情剧",
					"Title": "《克拉恋人》"
				}
			}
		}`)
		var actress Actress
		errActor := json.Unmarshal(jsonData, &actress)
		if errActor != nil {
			fmt.Println("error", errActor)
			return
		}
		fmt.Printf("姓名：%s\n", actress.Name)
		fmt.Printf("生日：%s\n", actress.Birthday)
		fmt.Printf("出生地：%s\n", actress.BirthPlace)
		fmt.Println("作品：")
		for index, value := range actress.Opus {
			fmt.Printf("\t日期：%s\n", index)
			fmt.Printf("\t\t分类：%s\n", value.Type)
			fmt.Printf("\t\t标题：%s\n", value.Title)
		}
	*/
	/*
		s := Student{
			Id:          1,
			StudentName: "小红",
			Results: []ReportResult{
				ReportResult{
					SubjectName: "语文",
					Score:       90,
				},
				ReportResult{
					SubjectName: "数学",
					Score:       100,
				},
			},
		}
		dataJson, _ := json.Marshal(s)
		//写文件
		ioutil.WriteFile("data2.json", dataJson, 0755)
	*/
	
	/*
	
		//movieOne := Movie
		movie := Movie{"新剧之王", 2000, 199, []string{"xingye", "yjy"}}
		//编码的过程   结构体------>json
		jsonStr, err := json.Marshal(movie)
		if err != nil {
			fmt.Println("json marshal error", err)
			return
		}
		fmt.Printf("jsonStr=%s\n", jsonStr)
	
		var (
			d5 = `{"a": "b"}`
			v5 map[string]string
			v6 interface{}
		)
		json.Unmarshal([]byte(d5), &v5)
		//printHelper("d5", v5)
	
		json.Unmarshal([]byte(d5), &v6)
		//printHelper("d5(interface{})", v6)
	
	*/
	
	/*
		s := [...]int{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}
		fmt.Println("打印s:", s[:])
		//fmt.Printf("数组=%s\n", s[:])
		var (
			data  = `1`
			value int
		)
	
		err1 := json.Unmarshal([]byte(data), &value)
		fmt.Println("Unmarshal error is: ", err1)
		fmt.Printf("Unmarshal value is %T,%d \n", value, value)
	
		value2, err2 := json.Marshal(value)
		fmt.Println("Marshal error is:", err2)
		fmt.Printf("Marshal value is:%T, %s \n", value, string(value2))
		//在这个例子中，我们Unmarshal和Marshal将一个整数的JSON二进制转化为go int数据
		//在这个例子中value类型由int修改为string后再次运行代码，你将得到：
		//Unmarshal error is: json: cannot unmarshal number into Go value of type string的错误提醒
	*/
}

func structJudgeNilTest(dataStruct interface{}) {
	if dataStruct != nil {
		fmt.Println("代表结构体不为空", dataStruct)
	} else {
		fmt.Println("代表结构体为空")
	}
}

// 成绩单
type ReportResult struct {
	SubjectName string  `json:"name"`
	Score       float64 `json:"score"`
}

type Student struct {
	Id          int            `json:"id"`
	Results     []ReportResult `json:"results"`
	StudentName string         `json:"stuname"`
}

//班级class
